<?php

namespace app\admin\model\store;

use think\Cache;
use think\Model;

/**
 * 文章分类模型
 * Class Category
 * @package app\store\model\article
 */
class Industry extends Model
{
    protected $name = 'industry';

    /**
     * 所有分类
     * @return mixed
     */
    public static function getALL()
    {
        $model = new static;
        if (!Cache::get('store_industry')) {
            $data = $model->order(['sort' => 'asc', 'create_time' => 'asc'])->select();
            $all = !empty($data) ? $data->toArray() : [];
            Cache::tag('cache')->set('store_industry', $all);
        }
        return Cache::get('store_industry');
    }

    public static function getPidALL()
    {
        $data = self::where('pid',0)->order(['sort' => 'asc', 'create_time' => 'asc'])->select();
        return $all = !empty($data) ? $data->toArray() : [];
    }

    /**
     * 分类详情
     * @param $category_id
     * @throws \think\exception\DbException
     */
    public static function detail($category_id)
    {
        return static::get($category_id);
    }

    /**
     * 添加新记录
     * @param $data
     * @return false|int
     */
    public function add($data)
    {

        $this->deleteCache();
        return $this->allowField(true)->save($data);
    }

    /**
     * 编辑记录
     * @param $data
     * @return bool|int
     */
    public function edit($data)
    {
        $this->deleteCache();
        return $this->allowField(true)->save($data);
    }

    /**
     * 删除商品分类
     * @param $category_id
     * @return bool|int
     */
    public function remove($category_id)
    {

        $this->deleteCache();
        return $this->delete();
    }

    /**
     * 删除缓存
     * @return bool
     */
    private function deleteCache()
    {
        return Cache::rm('store_industry');
    }

}
